package com.yanqu.road.server.cmd.house;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogHouseBankSilver;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.house.HouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.HouseModule;
import com.yanqu.road.server.manger.config.HouseMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_HOUSE_LUCK_SILVER, desc = "使用财运值")
public class HouseBankLuckSilverCmd extends AbstractCommand {


	private int isOneKey;
	private int pos;
	private String silver;

	private HouseProto.HouseBankLuckSilverRespMsg.Builder respMsg = HouseProto.HouseBankLuckSilverRespMsg.newBuilder();

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HouseProto.HouseBankLuckSilverReqMsg reqMsg = HouseProto.HouseBankLuckSilverReqMsg.parseFrom(packet.getBytes());
		isOneKey = reqMsg.getOneKey();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		HouseModule houseModule = player.getModule(HouseModule.class);
		int luckValue = houseModule.getTotalLuckValue();
		if(luckValue < HouseMgr.getLuckSilverUseValue()){
			ret = GameErrorCode.E_HOUSE_BANK_LUCK_VALUE_NO_ENOUGH;
			respMsg.setRet(ret);
			return;
		}
		respMsg.setRet(0);
		if(isOneKey == 1){
			for (;houseModule.getTotalLuckValue() >= HouseMgr.getLuckSilverUseValue();){
				oneTime(player);
				HouseProto.HouseBankLuckSilverTempMsg.Builder tempMsg = HouseProto.HouseBankLuckSilverTempMsg.newBuilder();
				tempMsg.setPos(pos);
				tempMsg.setSilver(silver);
				respMsg.addRewardList(tempMsg);
			}
		}else{
			oneTime(player);
			HouseProto.HouseBankLuckSilverTempMsg.Builder tempMsg = HouseProto.HouseBankLuckSilverTempMsg.newBuilder();
			tempMsg.setPos(pos);
			tempMsg.setSilver(silver);
			respMsg.addRewardList(tempMsg);
		}
		houseModule.syncUserHouseBank();
	}

	private void oneTime(GamePlayer player){
		HouseModule houseModule = player.getModule(HouseModule.class);
		pos = houseModule.luckSilverGet();
		int stime = HouseMgr.getLuckSilverTime(pos);
		BigInteger silverTemp = player.getUserInfo().getEarnSpeed().multiply(BigInteger.valueOf(stime));
		silver = silverTemp.toString();
		player.getModule(CurrencyModule.class).addCurrency(new Property(GameConfig.GAME_MONEY_SILVER,silverTemp), eLogMoneyType.HouseBank,eLogMoneyType.HouseBankLuckSilverReward);
		AutoLogMgr.add(new LogHouseBankSilver(
				player.getUserInfo().getUserId(),
				silver,
				pos,
				player.getUserInfo().getVipExp(),
				player.getUserInfo().getHouseLv()
		));
	}

	@Override
	public void sendData(GamePlayer player) {
		player.sendPacket(Protocol.U_HOUSE_LUCK_SILVER, respMsg);
	}
}
